package com.scitc.pleasurevideo.util;

import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scitc.pleasurevideo.datasql.VideoSourceSqlData;
import com.scitc.pleasurevideo.pojo.MenuTvVo;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static com.scitc.pleasurevideo.data.DataUtil.countOfSubstr;

public class MenuUrlUtil {

    private static MenuTvListListenr menuTvListListenr;

    public interface MenuTvListListenr {
        void menuTvListListenr(List<MenuTvVo> menuTvPojoList);

        void menuTvIntroduction(String introduction);
    }

    public static void setMenuTvListListenr(MenuTvListListenr menuTvList) {
        menuTvListListenr = menuTvList;
    }

    /**
     * 获取视频列表
     *
     * @param context
     * @param html_url
     * @param string_path
     * @param search_string
     * @return
     */
    public static void getMenuUrlOkgo(final Context context, final String html_url, final String string_path, final String search_string) {
//        final String html_url="https://www.mbkkk.com";
//        String url=html_url+"/index.php?m=vod-search-pg-1-wd-爱情公寓5";

        String url = html_url + string_path + search_string;

        final List<MenuTvVo> menuTvPojoList = new ArrayList<>();

        OkGo.<String>get(url)
                .tag(context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //先判断有没有
                        int frist_len = response.body().indexOf("xing_vb");
                        if (frist_len < 0) {
                            return;
                        }

                        //找到名字
                        String sss = response.body();
                        int t1 = response.body().indexOf("<title");
                        int t2 = response.body().indexOf("</title>");
                        String title = "";
                        if (t1 > 0 && t2 > 0) {
                            t1 = response.body().indexOf(">", t1);
                            title = response.body().substring(t1 + 1, t2);
                        }

                        //找到区间
                        int end_len = frist_len;

                        while (true) {

                            int s = response.body().indexOf("</div>", end_len);
                            int l = response.body().indexOf("<div", end_len);

                            end_len = s + 6;

                            if (l > s) {
                                break;
                            }

                        }

                        String long_string = response.body().substring(frist_len, end_len);
//                    LogUtil.dLong("????",long_string);


                        //判断搜索到几个内容
                        int num = countOfSubstr(long_string, "<span class=\"xing_vb4\">");

                        end_len = 0;
                        for (int i = 0; i < num; i++) {

                            MenuTvVo menuTvPojo = new MenuTvVo();

                            int titleLen = title.indexOf("-");
                            if (titleLen > 0) {
                                title = title.substring(0, titleLen);
                            }
                            titleLen = title.indexOf("－");
                            if (titleLen > 0) {
                                title = title.substring(0, titleLen);
                            }
                            menuTvPojo.setTitle(title);

                            menuTvPojo.setUrl(html_url);

                            //找到播放路径
                            int flen = long_string.indexOf("<a href=", end_len);
                            int elen = long_string.indexOf(" target=", flen);
                            menuTvPojo.setPlayerString(long_string.substring(flen + 9, elen - 1));

                            //找到名字
                            flen = elen + 17;
                            elen = long_string.indexOf("<", flen);
                            menuTvPojo.setName(long_string.substring(flen, elen));

                            //找到类型
                            flen = long_string.indexOf("xing_vb5", elen);
                            elen = long_string.indexOf("</span>", flen);
                            menuTvPojo.setType(long_string.substring(flen + 10, elen));

                            //找到时间
                            flen = long_string.indexOf("xing_vb6", elen);
                            elen = long_string.indexOf("</span>", flen);
                            menuTvPojo.setTime(long_string.substring(flen + 10, elen));

                            end_len = elen;

                            menuTvPojoList.add(menuTvPojo);
                        }

//                    Log.d("????", "menuTvPojoList:" + menuTvPojoList);

                        menuTvListListenr.menuTvListListenr(menuTvPojoList);

                    }
                });

    }


    public static List<VideoSourceVo> getUrlApiList() {
        List<VideoSourceVo> urlApiPojoList=VideoSourceSqlData.getDbDataManager().selectVideoSourceList();

        for(int i=0;i<urlApiPojoList.size();i++){
            if(!urlApiPojoList.get(i).getSource_url_is_select()){
                urlApiPojoList.remove(i);
                i--;
            }
        }
        return urlApiPojoList;
    }


    public static void getTvIntroduction(String id, Context context) {

        String url = "https://movie.douban.com/subject/" + id;

        OkGo.<String>get(url).tag(context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        int flen = response.body().indexOf("v:summary");
                        if (flen < 0) {
                            return;
                        }
                        flen = response.body().indexOf(">", flen);
                        int elen = response.body().indexOf("</span>", flen);

                        String tempstring = response.body().substring(flen + 1, elen);

                        String temp = tempstring.replaceAll("<br />", "");
                        tempstring = temp.replaceAll("　", "");
                        temp = tempstring.replaceAll(" ", "");
                        menuTvListListenr.menuTvIntroduction(temp.trim());
                    }
                });
    }


}
